package com.gp.service.impl;

import java.io.Serializable;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gp.mapper.BaseGoodsMapper;
import com.gp.mapper.BaseGoodsPropertyMapper;
import com.gp.model.dto.Appraises;
import com.gp.model.dto.GoodsDetailDto;
import com.gp.model.entity.BaseGoods;
import com.gp.model.entity.BaseGoodsProperty;
import com.gp.service.IBaseGoodsService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 弓成龙
 * @since 2017-11-26
 */
@Service
public class BaseGoodsServiceImpl extends ServiceImpl<BaseGoodsMapper, BaseGoods> implements IBaseGoodsService {

  @Autowired
  private BaseGoodsMapper goodsMapper;
  @Autowired
  private BaseGoodsPropertyMapper goodsPropertyMapper;

  @Override
  public boolean insertGoods(GoodsDetailDto goods) {
    // 首先插入商品
    BaseGoods info = goods.obtainBaseGoodsInfo();
    Long result = goodsMapper.insertGood(info);
    // 插入商品的属性信息
    BaseGoodsProperty property = goods.obtainBaseGoodsProperty();
    property.setGoodsId(info.getId());
    goodsPropertyMapper.insert(property);
    return result > 0;
  }
  
  @Override
  public GoodsDetailDto selectGoodsById(Serializable id) {
    return goodsMapper.selectGoodsById(id);
  }
  
  @Override
  public Page<BaseGoods> listGoods(Page<BaseGoods> page) {
    return page.setRecords(goodsMapper.listGoods(page, page.getCondition()));
  }

  @Override
  public boolean deleteById(String id) {
    Wrapper<BaseGoodsProperty> wrapper = new EntityWrapper<BaseGoodsProperty>();
    wrapper.eq("goodsId", id);
    // 删除商品属性表中对应的属性
    goodsPropertyMapper.delete(wrapper);
    // 删除商品表中的条目
    return goodsMapper.deleteById(Long.parseLong(id)) > 0;
  }

  @Override
  public Page<Appraises> listGoodsAppraises(Page<Appraises> page,Map condition) {
    page.setRecords(goodsMapper.listGoodsAppraises(page,condition));
    return page;
  }

}